/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kurvykurvy.core.ui;

import com.kurvykurvy.core.KurvyDataManager;
import com.kurvykurvy.page.BookTemplate;
import com.kurvykurvy.page.PageLayoutTemplate;
import com.kurvykurvy.page.PageTemplateSlide;
import com.kurvykurvy.ui.layout.VerticalLayout;
import java.awt.Color;
import java.util.Iterator;
import java.util.Vector;
import java.util.logging.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;

/**
 * Top component which displays something.
 */
@ConvertAsProperties( dtd = "-//com.kurvykurvy.core.ui//PageLayoutTemplate//EN",
autostore = false )
public final class PageLayoutTemplateTopComponent extends TopComponent
{

    private static PageLayoutTemplateTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "PageLayoutTemplateTopComponent";
    protected BookTemplate bookTemplate;
    protected Vector<PageLayoutTemplate> templates;

    public PageLayoutTemplateTopComponent()
    {
        initComponents();
        setName( NbBundle.getMessage( PageLayoutTemplateTopComponent.class, "CTL_PageLayoutTemplateTopComponent" ) );
        setToolTipText( NbBundle.getMessage( PageLayoutTemplateTopComponent.class, "HINT_PageLayoutTemplateTopComponent" ) );
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));
        putClientProperty( TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE );
        putClientProperty( TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE );
        putClientProperty( TopComponent.PROP_SLIDING_DISABLED, Boolean.TRUE );
        putClientProperty( TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE );
        putClientProperty( TopComponent.PROP_KEEP_PREFERRED_SIZE_WHEN_SLIDED_IN, Boolean.TRUE );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        templateFilmView = new javax.swing.JScrollPane();
        templateFilmStrip = new javax.swing.JPanel();

        setMaximumSize(new java.awt.Dimension(180, 32767));
        setMinimumSize(new java.awt.Dimension(180, 32767));

        templateFilmView.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        templateFilmStrip.setBackground( Color.WHITE );
        templateFilmStrip.setMaximumSize(new java.awt.Dimension(200, 32767));
        templateFilmStrip.setMinimumSize(new java.awt.Dimension(180, 1024));
        templateFilmStrip.setPreferredSize(new java.awt.Dimension(180, 32763));
        templateFilmStrip.setLayout(null);
        templateFilmView.setViewportView(templateFilmStrip);
        templateFilmStrip.setLayout( new VerticalLayout() );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(templateFilmView, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(templateFilmView, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE, Short.MAX_VALUE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel templateFilmStrip;
    private javax.swing.JScrollPane templateFilmView;
    // End of variables declaration//GEN-END:variables

    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized PageLayoutTemplateTopComponent getDefault()
    {
        if ( instance == null )
        {
            instance = new PageLayoutTemplateTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the PageLayoutTemplateTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized PageLayoutTemplateTopComponent findInstance()
    {
        TopComponent win = WindowManager.getDefault().findTopComponent( PREFERRED_ID );
        if ( win == null )
        {
            Logger.getLogger( PageLayoutTemplateTopComponent.class.getName() ).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system." );
            return getDefault();
        }
        if ( win instanceof PageLayoutTemplateTopComponent )
        {
            return (PageLayoutTemplateTopComponent) win;
        }
        Logger.getLogger( PageLayoutTemplateTopComponent.class.getName() ).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior." );
        return getDefault();
    }

    @Override
    public int getPersistenceType()
    {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened()
    {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed()
    {
        // TODO add custom code on component closing
    }

    void writeProperties( java.util.Properties p )
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty( "version", "1.0" );
        // TODO store your settings
    }

    Object readProperties( java.util.Properties p )
    {
        PageLayoutTemplateTopComponent singleton = PageLayoutTemplateTopComponent.getDefault();
        singleton.readPropertiesImpl( p );
        return singleton;
    }

    private void readPropertiesImpl( java.util.Properties p )
    {
        String version = p.getProperty( "version" );
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID()
    {
        return PREFERRED_ID;
    }

    public BookTemplate getBookTemplate()
    {
        return bookTemplate;
    }

    public void setBookTemplate( BookTemplate book )
    {
        this.bookTemplate = book;
        refreshStrip();
    }

    public Vector<PageLayoutTemplate> getTemplates()
    {
        if ( this.templates == null && this.bookTemplate != null )
            this.templates = new Vector( this.bookTemplate.getPageTemplates() );
        else
            this.templates = new Vector<PageLayoutTemplate>();

        return this.templates;
    }

    public void refreshStrip()
    {
        this.templateFilmStrip.removeAll();

        for ( Iterator<PageLayoutTemplate> it = getTemplates().iterator(); it.hasNext(); )
        {
            PageLayoutTemplate template = it.next();
            PageTemplateSlide slide = new PageTemplateSlide( template );

            this.templateFilmStrip.add( slide );
        }
        this.repaint( this.templateFilmStrip.getBounds() );
    }
}
